﻿using System;
using System.Collections.Generic;
using System.Text;
using JinTai.Gravestones.Interfaces;
using NHibernate;
using Ornament.Util.NHibernateHelper;

namespace JinTai.Gravestones.Dao.NHibernateImple
{
    public class ContactSearcher 
    {
        #region IContactSearcher Members

        public string HomePhone { get; set; }

        public string Mobile { get; set; }

        public string Address { get; set; }

        #endregion

        protected int GetCout()
        {
            string s = CreateHql();
            IQuery query = base.session.CreateQuery("select count(*) " + s);
            return query.List<Int32>()[0];
        }

        protected string CreateHql()
        {
            var sb = new StringBuilder("From Contact c");
            bool searchCondition = false;
            if (!String.IsNullOrEmpty(HomePhone))
            {
                searchCondition = true;
                sb.Append(" where c.HomePhone=:HomePhone");
            }
            if (!String.IsNullOrEmpty(Mobile))
            {
                if (!searchCondition)
                {
                    searchCondition = true;
                    sb.Append(" where ");
                }
                else
                {
                    sb.Append(" and ");
                }
                sb.Append("c.Mobile=:Mobile");
            }
            if (!string.IsNullOrEmpty(Address))
            {
                if (!searchCondition)
                {
                    sb.Append(" where ");
                }
                else
                {
                    sb.Append(" and ");
                }
                sb.Append(" c.Address like %" + Address + "%");
            }

            return sb.ToString();
        }

     

      
    }
}